<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <!-- layui中的核心css文件 -->
    <link rel="stylesheet" href="layui-v2.5.7/layui/css/layui.css">
    <!-- swiper的核心css文件 -->
    <link rel="stylesheet" href="swiper/css/swiper.css">
    <!-- 自己的css文件 -->
    <link rel="stylesheet" href="./css/login.css">
    <!-- 数据文件 -->
    <script src="dataJs/data.js"></script>
    <!-- layui中的核心js文件 -->
    <script src="layui-v2.5.7/layui/layui.js"></script>
    <!-- swiper的核心js文件 -->
    <script src="swiper/js/swiper.js"></script>
    <!-- jquery库文件 -->
    <script src="jquery/jquery 1.11.0/jquery-1.11.0.js"></script>
    <!-- 自己的js文件 -->
    <!-- <script src=""></script> -->
    <script>
        var arrs = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "U", "W", "X", "Y", "Z"]
        function getMa() {
            var p = '' + arrs[parseInt(Math.random() * 52)] + arrs[parseInt(Math.random() * 52)] + arrs[parseInt(Math.random() * 52)] + arrs[parseInt(Math.random() * 52)];
            $("#getMa").html(p);
        }
    </script>
</head>

<body>
    <div class="layui-container" id="loginFrom">
        <h2>登陆用户</h2>
        <form id="formDemo2" class="layui-form" action="./index.html">
            <div class="layui-form-item">
                <label class="layui-form-label">用户名</label>
                <div class="layui-input-inline inputLength">
                    <input id="usernameLogin" type="text" name="username" required lay-verify="usernameLogin"
                        placeholder="请输入用户名" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">密码</label>
                <div class="layui-input-inline inputLength">
                    <input id="passwordLogin" type="password" name="password" required lay-verify="passwordLogin"
                        placeholder="请输入密码" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">验证码</label>
                <div class="layui-input-inline">
                    <input id='maLogin' type="text" name="yanzhengma" required lay-verify="maLogin" placeholder="请输入验证码"
                        autocomplete="off" class="layui-input">
                </div>
                <div id="getMa" class="layui-form-mid layui-word-aux yanzheng" onclick='getMa()'>获取验证码</div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn btnLength" lay-submit lay-filter="formDemo">登录</button>
                </div>
            </div>
        </form>
        <div class="lianjie"><a href="./register.html">没有账户，请注册</a></div>
    </div>
    <script>
        layui.use('form', function () {
            var form = layui.form;
            var users = JSON.parse(localStorage.getItem("users"));//将获取到字符串转换数组
            var indexx;
            form.verify({
                usernameLogin: function (value) {
                    var flag = false;
                    var result = users.some(function (item, index) {
                        if (value == item.username) {
                            indexx = index;
                            console.log(index);
                            console.log(indexx)
                            flag = true;
                        }
                    })
                    if (flag == false) {
                        return "用户名不存在"
                    }
                }
                , passwordLogin: function (value) {
                    if (users[indexx].password != value) {
                        return "用户名与密码不匹配";
                    }
                }
                , maLogin: function (value) {
                    // console.log($("div[id=getMa]").html())
                    if ($("div[id=getMa]").html() != value) {
                        return "验证码错误";
                    }
                }
            });

            //事件监听
            form.on('submit(formDemo)', function (data) {//验证表单的名称在注册安缪附近，不是整个表单
                localStorage.setItem("index", indexx)
                console.log(indexx)
                // return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
                return true;

            });
        });
    </script>
</body>

</html>